package com.xyh.controller;

import com.google.gson.Gson;
import com.xyh.bean.Insect;
import com.xyh.service.IInsectService;
import com.xyh.service.impl.InsectServiceImpl;
import com.xyh.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;

/**
 * @Author: XiongYuhao
 * @Date: 2020/10/23 0023-上午 10:44
 * @Description: MyForestManagerSystem
 * @version：1.0
 */
@WebServlet(name = "InsectServlet",urlPatterns = "/is.do")
public class InsectServlet extends HttpServlet {
    IInsectService iis = new InsectServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method.equalsIgnoreCase("insect_detail_page")){
            //虫害详情分页
            insectDetailByPage(request,response);
        }else if(method.equalsIgnoreCase("add_insect")){
            //添加虫害
            addInsect(request,response);
        }else if(method.equalsIgnoreCase("delete_insect")){
            //清除虫害
            deleteInsect(request,response);
        }else if(method.equalsIgnoreCase("update_insect")){
            //更新虫害
            updateInsect(request, response);
        }
    }
    private void updateInsect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("insectId");
        int insectId = 0;
        if(sid != null){
            insectId = Integer.parseInt(sid);
        }
        String insectName = request.getParameter("insectName");
        String breed = request.getParameter("breed");
        String host = request.getParameter("host");
        String enemy = request.getParameter("enemy");
        String danger = request.getParameter("danger");
        String measures = request.getParameter("measures");
        String larvapic = request.getParameter("larvapic");
        String adultpic = request.getParameter("adultpic");
        Insect insect = new Insect(insectId,insectName,breed,host,enemy,danger,measures,larvapic,adultpic);
        boolean flag = iis.update(insect);
        response.getWriter().write(String.valueOf(flag));
    }
    private void deleteInsect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String insectId = request.getParameter("insectId");
        int id = 0;
        if(insectId != null){
            id = Integer.parseInt(insectId);
        }
        boolean flag = iis.delete(id);
        response.getWriter().write(String.valueOf(flag));
    }
    private void addInsect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String insectName = request.getParameter("insectName");
        String breed = request.getParameter("breed");
        String host = request.getParameter("host");
        String enemy = request.getParameter("enemy");
        String danger = request.getParameter("danger");
        String measures = request.getParameter("measures");
        String larvapic = request.getParameter("larvapic");
        String adultpic = request.getParameter("adultpic");
        if(insectName.equalsIgnoreCase("")  || breed.equalsIgnoreCase("")  || host.equalsIgnoreCase("")  || enemy.equalsIgnoreCase("")  ||
                danger.equalsIgnoreCase("")  || measures.equalsIgnoreCase("")  || larvapic.equalsIgnoreCase("") || adultpic.equalsIgnoreCase("") ){
            return;
        }
        Insect insect = new Insect(insectName,breed,host,enemy,danger,measures,larvapic,adultpic);
        boolean b = iis.addInsect(insect);
        Gson gson = new Gson();
        String json = gson.toJson(b);
        response.getWriter().write(json);
    }
    private void insectDetailByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得前端的pageNo
        String pn = request.getParameter("pageNo");
        int pageNo = 1;
        if(pn == null){
            pageNo = 1;
        }else{
            pageNo = Integer.parseInt(pn);
        }
        //获得前端的pageSize
        String ps = request.getParameter("pageSize");
        int pageSize = Integer.parseInt(ps);
        //获得前端的害虫名
        String insectName = request.getParameter("insectName");
        //获得前端的寄主
        String host = request.getParameter("host");
        //显示所有虫害信息并分页
        PageUtil<Insect> pageBean = iis.listsByPage(pageNo, pageSize,insectName,host);
        Gson gson = new Gson();
        String json = gson.toJson(pageBean);
        response.getWriter().write(json);
    }
}
